import time

from telemetrix import telemetrix

ENABLE_PIN =  8

XDIRPIN = 5;     # x方向控制引脚
XSTEPPIN = 2;    # x步进控制引脚
YDIRPIN = 6;     # y方向控制引脚
YSTEPPIN = 3;    # y步进控制引脚

board = telemetrix.Telemetrix()

board.set_pin_mode_digital_output(pin_number=ENABLE_PIN)

board.digital_write(pin=ENABLE_PIN,value=0)

motorX = board.set_pin_mode_stepper(interface=1, pin1=XSTEPPIN,pin2=XDIRPIN,  enable=True)
motorY = board.set_pin_mode_stepper(interface=1, pin1=YSTEPPIN,pin2=YDIRPIN,  enable=True)
print(motorX)
print(motorY)

board.stepper_set_max_speed(motor_id=0,max_speed=500)
board.stepper_set_speed(motor_id=0,speed=500)
board.stepper_set_acceleration(motor_id=0,acceleration=500)

board.stepper_set_speed(motor_id=1,speed=1000)
board.stepper_set_max_speed(motor_id=1,max_speed=1000)
board.stepper_set_acceleration(motor_id=1,acceleration=1000)


exit_flag = 0

board.stepper_set_current_position(motor_id=0, position=0)
board.stepper_move_to(motor_id=0, position=1024)

board.stepper_set_current_position(motor_id=1, position=0)
board.stepper_move_to(motor_id=1, position=1024)

def the_callback(data):
    global exit_flag
    date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data[2]))
    print(f'Motor {data[1]} absolute motion completed at: {date}.')
    exit_flag += 1


if __name__ == '__main__':



    board.stepper_run(motor_id=0,completion_callback=the_callback)

    board.stepper_run(motor_id=1, completion_callback=the_callback)

    # while True:
    #     time.sleep(.001)



